package com.karst87.study.dowithbytes.chapter01
{
	import com.karst87.study.dowithbytes.utils.BytesUtils;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import org.aswing.Container;
	import org.aswing.EmptyLayout;
	import org.aswing.FlowLayout;
	import org.aswing.JButton;
	import org.aswing.JComboBox;
	import org.aswing.JPanel;
	import org.aswing.JTextField;

	internal class BitwiseOperators extends JPanel
	{
		public function BitwiseOperators()
		{
			super( new EmptyLayout() ) ;
			createView() ;
			test() ;
		}

		private var num1TF : JTextField = null ;
		private var num2TF : JTextField = null ;
		private var num3TF : JTextField = null ;
		private var type1Comb : JComboBox = null ;
		private var type2Comb : JComboBox = null ;
		private var type3Comb : JComboBox = null ;
		private var operatorComb : JComboBox = null ;
		private var executeBtn : JButton = null ;
		private function createView() : void
		{
			if( num1TF == null )
				num1TF = new JTextField( "0" ) ;
			num1TF.setSizeWH( 240, 25 ) ;
			num1TF.setLocationXY( 10 , 5 ) ;

			if( num2TF == null )
				num2TF = new JTextField( "0" ) ;
			num2TF.setSizeWH( 240, 25 ) ;
			num2TF.setLocationXY( 10 , 40 ) ;

			if( num3TF == null )
				num3TF = new JTextField( "0" ) ;
			num3TF.setSizeWH( 290, 25 ) ;
			num3TF.setLocationXY( 10 , 75 ) ;
			
			if( type1Comb == null )
				type1Comb = new JComboBox() ;
			type1Comb.setSizeWH( 40, 25 ) ;
			type1Comb.setLocationXY( 260 , 5 ) ;
			type1Comb.setListData( [2, 8, 10, 16] ) ;
			type1Comb.setSelectedIndex( 2 ) ;
			
			if( type2Comb == null )
				type2Comb = new JComboBox() ;
			type2Comb.setSizeWH( 40, 25 ) ;
			type2Comb.setLocationXY( 260 , 40 ) ;
			type2Comb.setListData( [2, 8, 10, 16] ) ;
			type2Comb.setSelectedIndex( 2 ) ;
			
			if( type3Comb == null )
				type3Comb = new JComboBox() ;
			type3Comb.setSizeWH( 40, 25 ) ;
			type3Comb.setLocationXY( 310 , 75 ) ;
			type3Comb.setListData( [2, 8, 10, 16] ) ;
			type3Comb.setSelectedIndex( 2 ) ;
			
			if( operatorComb == null )
				operatorComb = new JComboBox() ;
			operatorComb.setSizeWH( 60, 25 ) ;
			operatorComb.setLocationXY( 310 , 40 ) ;
			operatorComb.setListData( ["&", "<<", "~", "|", ">>", ">>>", "^"] ) ;
			operatorComb.setSelectedIndex( 0 ) ;
			
			if( executeBtn == null )
				executeBtn = new JButton( "Execute" ) ;
			executeBtn.setSizeWH( 60, 25 ) ;
			executeBtn.setLocationXY( 380 , 40 ) ;
			executeBtn.addEventListener( MouseEvent.CLICK, executeHandler ) ;
			
			appendAll( num1TF, num2TF, type1Comb, type2Comb, type3Comb, operatorComb, executeBtn, num3TF ) ;
		}

		private function executeHandler( $event : MouseEvent = null ) : void
		{
			var num1 : * = BytesUtils.covertBinary( num1TF.getText(), type1Comb.getSelectedItem()) ;
			var num2 : * = BytesUtils.covertBinary( num2TF.getText(), type2Comb.getSelectedItem()) ;
			switch( operatorComb.getSelectedItem() )
			{
				case "&" :
					num3TF.setText( String(num1&num2) ) ;
					break;
				case "<<" :
					num3TF.setText( String(num1<<num2) ) ;
					break;
				case "~" :
					num3TF.setText( String(~num1) ) ;
					break;
				case "|" :
					num3TF.setText( String(num1|num2) ) ;
					break;
				case ">>" :
					num3TF.setText( String(num1>>num2) ) ;
					break;
				case ">>>" :
					num3TF.setText( String(num1>>>num2) ) ;
					break;
				case "^" :
					num3TF.setText( String(num1^num2) ) ;
					break;
					
				default:
				{
					break;
				}
			}
			num3TF.setText( BytesUtils.covertBinary(num3TF.getText(), 10 ).toString( type3Comb.getSelectedItem() ) ) ;
		}
		
		
		
		/**
		 * 在性能方面有非常轻微的差别
		 */
		private function test() : void
		{
			var startTime : Number = getTimer() ;
			var value : int = 0 ;
			var result : int = 0 ;
			for( var i : int = 0 ; i < 5000000; i++ )
			{
				result = value / 4 ;
			}
			trace( result ) ;
			trace( getTimer() - startTime ) ;

			startTime = getTimer() ;
			value = 0 ;
			result = 0 ;
			for( i = 0 ; i < 5000000; i++ )
			{
				result = value >> 2 ;
			}
			trace( result ) ;
			trace( getTimer() - startTime ) ;
		}
	}
}